package com.wjh.graduation.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wjh.graduation.domain.Groups;
import com.wjh.graduation.domain.R;
import com.wjh.graduation.domain.User;
import com.wjh.graduation.service.GroupsService;
import com.wjh.graduation.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Wu Jiahong
 * @since 2022-01-25
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户相关Api")
public class UserController {

	@Autowired
	UserService userService;

	@Autowired
	GroupsService groupsService;

	@PostMapping("/register")
	@ApiOperation("注册功能，点击注册会验证是否存在账户")
	@Transactional
	public R<User> register(@Validated @RequestBody @ApiParam(name = "传入user对应的2个字段名,无需id") User user) {
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(Strings.isNotEmpty(user.getAccount()), "account", user.getAccount());

		int count = userService.count(queryWrapper);
		if (count > 0) {
			return new R(300, "用户名存在");
		} else {
			if (userService.save(user)) {
				QueryWrapper<User> qw = new QueryWrapper<>();
				qw.eq("account", user.getAccount());
				//获得user的id
				User user1 = userService.getOne(qw);
				//创建账户时创建默认分组
				groupsService.save(new Groups(user1.getUserId(), "默认分组"));
				//创建搜索条件
				QueryWrapper<User> query = new QueryWrapper<>();
				query.eq("account", user.getAccount()).eq("password", user.getPassword());
				User u = userService.getOne(query);
				if (u != null) {
					return new R(200, u, "注册成功！");
				} else {
					throw new NullPointerException("查询用户失败，注册失败");
				}
			} else {
				return new R(303, "注册失败，请重试");
			}
		}
	}


	@PostMapping("/login")
	@ApiOperation("登录接口，账号密码是必填选项")
	public R login(@RequestParam String account, @RequestParam String password) {
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("account", account).eq("password", password);
		if (userService.count(queryWrapper) > 0) {
			//携带User的信息回传
			QueryWrapper<User> qw = new QueryWrapper<>();
			qw.eq("account", account);
			User user = userService.getOne(qw);
			return new R(200, user, "登录成功");
		} else {
			return new R(300, "请检查用户名或密码");
		}
	}

	@GetMapping("/info")
	@ApiOperation("获取用户信息")
	public R getUserInfo(Integer id) {
		QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
		queryWrapper.eq("user_id", id);
		User user = userService.getOne(queryWrapper);
		if (user != null) {
			return new R(200, user, "获取成功");
		} else {
			return new R(303, null, "获取失败");
		}

	}

	@PostMapping("/update")
	@Transactional
	@ApiOperation("更改用户信息")
	public R<User> update(@Validated @RequestBody @ApiParam(name = "传入User的账号、密码") User user) {
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(Strings.isNotEmpty(user.getAccount()), "account", user.getAccount());
		int count = userService.count(queryWrapper);
		if (count > 0) {
			//设置用户名不可修改
			if (userService.updateById(user)) {
				//创建搜索条件，修改完返回User的数据
				QueryWrapper<User> query = new QueryWrapper<>();
				query.eq("account", user.getAccount()).eq("password", user.getPassword());
				User u = userService.getOne(query);
				if (u != null) {
					return new R(200, u, "修改成功！");
				} else {
					throw new NullPointerException("查询用户失败，修改失败");
				}
			}
			return new R<User>(300, "用户名存在");
		} else {
			//账号不存在
			return new R<User>(303, "修改失败");
		}
	}

}

